package com.ufo.store.management.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.ufo.store.management.ui.fragment.CalenderFragment;
import com.ufo.store.management.ui.activity.PayMasterActivity;

import java.util.ArrayList;
import java.util.List;

public class CalenderViewPagerAdapter extends FragmentPagerAdapter {
    private final int mCount = PayMasterActivity.CALENDER_DAY_COUNT;
    private CalenderFragment.CalenderClickListener mCalenderClickListener = null;
    private FragmentManager mFragmentManager;

    public CalenderViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        mFragmentManager = fragmentManager;
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public Fragment getItem(int position) {
        CalenderFragment fragment = new CalenderFragment();
        fragment.setPosition(position);
        fragment.setCalenderClickListener(mCalenderClickListener);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    public void setCalenderClickListener( CalenderFragment.CalenderClickListener listener){
        mCalenderClickListener = listener;
    }

    public void setAccountsNumber(String accountsNum, ArrayList<String> hourValueList, ArrayList<String> scanValueList){
        List<Fragment>  calendarFragments = mFragmentManager.getFragments();
        if(calendarFragments!=null && calendarFragments.size()!=0){
            for(int i = 0; i< calendarFragments.size(); i++){
                CalenderFragment calendarFragment =  (CalenderFragment)calendarFragments.get(i);
                if(calendarFragment!=null&&calendarFragment.getUserVisibleHint()){
                    calendarFragment.setAccountsNumber(accountsNum,hourValueList,scanValueList);
                }
            }
        }
    }

    public void setRefresh(){
        List<Fragment>  calendarFragments = mFragmentManager.getFragments();
        if(calendarFragments!=null && calendarFragments.size()!=0){
            for(int i = 0; i< calendarFragments.size(); i++){
                CalenderFragment calendarFragment =  (CalenderFragment)calendarFragments.get(i);
                if(calendarFragment!=null&&calendarFragment.getUserVisibleHint()){
                    calendarFragment.setRefresh();
                }
            }
        }
    }
}
